package com.espirit.common;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.espirit.eap.manager.RevisionHistory;
import com.espirit.eap.sa.User;
import com.espirit.eap.sa.UserService;
import com.googlecode.cswish.annotation.Protected;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.oosearch.QLParse;
import com.googlecode.cswish.struts.conversion.DefaultDateConverter;
import com.googlecode.cswish.struts.spring.GenericService;

@Service
public class SysLogService {

	private static final Log logger = LogFactory.getLog(SysLogService.class);
	
	@Resource
	private GenericService genericService;

	@Resource
	protected QLParse qlParse;
	
	@Resource
	private UserService userService;
	
	@Resource
	private DefaultDateConverter dateConverter;
	
	@Protected
	public void add(SysLog model) {
		// add new record
		User owner = userService.getLoginUser();
		User operator = userService.getLoginUser();
		if (model.getContent() == null || model.getContent().equals("")) return ;
		
		SysLog newSysLog = new SysLog();
		newSysLog.setParty(model.getParty());
		newSysLog.setContent(model.getContent());
		newSysLog.setOwner(owner);
		if (operator != null){
			newSysLog.setOperator(operator.getCode());
		}
		genericService.add(newSysLog);
		
		// reset the display content
		model.setContent(null);
	}
	
	public void asyncOrigAndDestItem(Integer eventItemId, String content){
		Set<Integer> ids = new HashSet<Integer>();
		ids.remove(eventItemId);
		User user = userService.getLoginUser();
	}
	
	
	public SysLog add(SysLog model, boolean tiny) {
		add(model);
		
		// prepare the search result
		SysLog searchModel = new SysLog();
		searchModel.setParty(model.getParty());
		
		return search(searchModel, tiny);
	}

	public SysLog search(SysLog model, boolean tiny) {
		String hql = "";
		if (model.getParty() != null){
			hql = " from SysLog  a where a.party.id=" + model.getParty().getId();
		} 
		
		StringBuilder sb = new StringBuilder();
		
		if (hql.length() > 0) {
			QLInfo qlInfo = new QLInfo(hql, true); 
			qlParse.setSort(qlInfo, SysLog.class.getName(), model, "a.revisionHistory.createdOn");
			qlInfo.setDir("desc");
			qlInfo.setDisablePermissionFilter(false);
			List<SysLog> sysLogs = genericService.searchByQl(qlInfo, false).getData();
			
			for (SysLog sysLog : sysLogs) {
				RevisionHistory revisionHistory = sysLog.getRevisionHistory();
				if (revisionHistory != null && revisionHistory.getCreatedBy() != null) {
					String createDate = dateConverter.convertToString(null, revisionHistory.getCreatedOn(), true);
					createDate = createDate.substring(0, createDate.lastIndexOf(":"));
					String operator = sysLog.getOperator();
					if (operator == null){
						operator = revisionHistory.getCreatedBy().getName();
					}
					sb.append(String.format("%s [%s]: %s<br>", operator, createDate, sysLog.getContent()));
				}
			}
		} else {
			logger.error("Failed to read the SystemLog, invalid parameter: " + model);
		}
		
		SysLog sysLog = new SysLog();
		sysLog.setRichContent(sb.toString());
		return sysLog;
	}
}